package cn.melon.view.admin.course;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.melon.mod.sc.ScInfo;
import cn.melon.view.admin.course.ctr.AddCourseDlgCtr;

public class AddCourseDlg extends JDialog{

   private ActionListener listener;
    
    private static final long serialVersionUID = 1L;
    private JTextField classNum = new JTextField("",20);
    private JTextField className = new JTextField("",20);
    private JTextField teacherName = new JTextField("",20);
    private JTextField classTime = new JTextField("",20);
    private JTextField classroom = new JTextField("",20);
    
    private JButton saveButton = new JButton("保存");
    private JButton cancelButton = new JButton("取消");
    String[] textStr = {"课程号：", "课程名称：", "教师：", "课程时间：", "教室："};
    
    JTextField[] jTextFields = {classNum, className, teacherName, classTime, classroom};
    
    public AddCourseDlg(IUpdateCourseTable updateTable) {
        listener = new AddCourseDlgCtr(this, updateTable);
        initialize();
    }

    private void initialize(){
        this.setTitle("课程信息");
        this.setSize(400, 300);
       
        JPanel panel = new JPanel();
        
        GridBagLayout layout = new GridBagLayout();
        panel.setLayout(layout);        

        for(int i=0;i<textStr.length;i++){
            GridBagConstraints constraints = new GridBagConstraints();
            constraints.anchor = GridBagConstraints.EAST;
            constraints.fill = GridBagConstraints.NONE;
            constraints.gridx = 0;
            constraints.gridy = i;
            constraints.weightx = 100;
            constraints.weighty = 100;
            panel.add(new JLabel(textStr[i]),constraints);
            constraints.anchor = GridBagConstraints.CENTER;
            constraints.gridx = 1;
            panel.add(jTextFields[i],constraints);
            constraints.anchor = GridBagConstraints.EAST;
            constraints.gridx = 2;
            panel.add(Box.createRigidArea(new Dimension(2, 15)));
        }
        
        JPanel tPanel = new JPanel();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 1;
        constraints.gridy = textStr.length;
        constraints.weightx = 100;
        constraints.weighty = 100;
        tPanel.add(saveButton);     //按钮
        tPanel.add(Box.createRigidArea(new Dimension(10, 15)));
        tPanel.add(cancelButton);
        panel.add(tPanel,constraints);      //按钮
        
        this.add(panel);
        this.setResizable(false);

        saveButton.addActionListener(listener);
        cancelButton.addActionListener(listener);
    }
    
    public ScInfo getScInfo() {
        ScInfo info = new ScInfo();
        info.setClassTerm("16-17-2");
        info.setClassNumber(classNum.getText());
        info.setClassName(className.getText());
        info.setClassTeacher(teacherName.getText());
        info.setClassTime(classTime.getText());
        info.setClassroom(classroom.getText());
        return info;
    }
}
